﻿// Filename: ServiceBUS.cs
// Author: Giã Dương Đức Minh
// Email: giaduongducminh@gmail.com
// 
// Date created: 03/01/2012 (dd/mm/yyyy)
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Resources;
using System.Web;

namespace Ryder_US_Website.BUS
{
    /// <summary>
    /// Ryder_US_Website.BUS.ServiceBUS
    /// Team Breakers
    /// Author: Đức Minh GIÃ DƯƠNG (Breakers.Terry)
    /// Email: giaduongducminh@gmail.com
    /// </summary>
    public class ServiceBUS
    {
        public static string conString = ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString;

        //public static Breakers_RyderUSDataContext context =
        //    new Breakers_RyderUSDataContext(ConfigurationManager.ConnectionStrings["breakers_RyderUSConnectionString"].ConnectionString);

        #region Select
        /// <summary>
        /// Gets all services.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="rowsPerPage">The number of rows. Rows per page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_Service> GetAll(int curPage, int rowsPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowsPerPage < 1)
                rowsPerPage = 10;

            List<RUS_Service> list = new List<RUS_Service>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            orderby s.Service_name
                            select s;
                numPages = (query.Count()/rowsPerPage) + 1;
                list = query.Skip((curPage - 1)*rowsPerPage).Take(rowsPerPage).ToList<RUS_Service>();
            }
            return list;
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public static List<RUS_Service> GetAll()
        {
            List<RUS_Service> list = new List<RUS_Service>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            //orderby s.Service_name
                            select s;
                list = query.ToList<RUS_Service>();
            }
            return list;
        }

        /// <summary>
        /// Gets the active services.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="rowsPerPage">The number of rows. Rows per page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_Service> GetActiveServices(int curPage, int rowsPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowsPerPage < 1)
                rowsPerPage = 10;

            List<RUS_Service> list = new List<RUS_Service>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_trash == 0)
                            orderby s.Service_name
                            select s;
                numPages = (query.Count()/rowsPerPage) + 1;
                list = query.Skip((curPage - 1)*rowsPerPage).Take(rowsPerPage).ToList<RUS_Service>();
            }
            return list;
        }

        /// <summary>
        /// Gets the active services.
        /// </summary>
        /// <returns></returns>
        public static List<RUS_Service> GetActiveServices()
        {
            List<RUS_Service> list = new List<RUS_Service>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_trash == 0)
                            orderby s.Service_name
                            select s;
                list = query.ToList<RUS_Service>();
            }
            return list;
        }

        /// <summary>
        /// Gets the inactive services.
        /// </summary>
        /// <param name="curPage">The current page.</param>
        /// <param name="rowsPerPage">The number of rows. Rows per page.</param>
        /// <param name="numPages">The number of pages.</param>
        /// <returns></returns>
        public static List<RUS_Service> GetInactiveServices(int curPage, int rowsPerPage, ref int numPages)
        {
            if (curPage < 1)
                curPage = 1;
            if (rowsPerPage < 1)
                rowsPerPage = 10;

            List<RUS_Service> list = new List<RUS_Service>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_trash == 1)
                            orderby s.Service_name
                            select s;
                numPages = (query.Count()/rowsPerPage) + 1;
                list = query.Skip((curPage - 1)*rowsPerPage).Take(rowsPerPage).ToList<RUS_Service>();
            }
            return list;
        }

        /// <summary>
        /// Gets the inactive services.
        /// </summary>
        /// <returns></returns>
        public static List<RUS_Service> GetInactiveServices()
        {
            List<RUS_Service> list = new List<RUS_Service>();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_trash == 1)
                            orderby s.Service_name
                            select s;
                list = query.ToList<RUS_Service>();
            }
            return list;
        }

        /// <summary>
        /// Gets the service by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static RUS_Service GetServiceByID(int id)
        {
            RUS_Service sr = new RUS_Service();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_ID == id)
                            select s;
                sr = query.SingleOrDefault<RUS_Service>();
            }
            return sr;
        }

        /// <summary>
        /// Gets the active service by ID.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static RUS_Service GetActiveServiceByID(int id)
        {
            RUS_Service sr = new RUS_Service();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_ID == id && s.Service_trash == 0)
                            select s;
                sr = query.SingleOrDefault<RUS_Service>();
            }
            return sr;
        }

      
        #endregion

        #region Insert
        /// <summary>
        /// Inserts the specified service.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns>New service</returns>
        public static RUS_Service Insert(RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service != null)
                    context.RUS_Services.InsertOnSubmit(service);
                try
                {
                    context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể thêm dịch vụ mới được.");
                }
            }
            return service;
        }

        /// <summary>
        /// Inserts the specified name.
        /// </summary>
        /// <param name="name">The service's name.</param>
        /// <param name="description">The service's description.</param>
        /// <returns>New service</returns>
        public static RUS_Service Insert(String name, String description)
        {
            if (String.IsNullOrEmpty(name))
                throw new Exception("Tên dịch vụ không được bỏ trống.");
            RUS_Service service = new RUS_Service();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                service.Service_name = name;
                service.Service_description = description;
                service.Service_trash = 0;
                context.RUS_Services.InsertOnSubmit(service);
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể thêm dịch vụ mới được.");
                }
            }
            return service;
        }
        #endregion

        #region Update
        /// <summary>
        /// Update the specified service.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns>Updated service.</returns>
        public static RUS_Service Update(RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                try
                {
                    if (service != null)
                    {
                        context.SubmitChanges();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật dịch vụ được.");
                }
            }
            return service;
        }

        /// <summary>
        /// Update the specified service.
        /// </summary>
        /// <param name="name">The service's name.</param>
        /// <param name="description">The service's description.</param>
        /// <param name="id">The id of service need updating.</param>
        /// <returns>Updated service</returns>
        public static RUS_Service Update(String name, String description,int trash, int id)
        {
            if (String.IsNullOrEmpty(name))
                throw new Exception("Tên dịch vụ không được bỏ trống.");
            RUS_Service service = new RUS_Service();
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                service.Service_ID = id;
                service.Service_name = name;
                service.Service_description = description;
                service.Service_trash = trash;
                try
                {
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể cập nhật dịch vụ được.");
                }
            }
            return service;
        }
        #endregion

        #region Delete & Restore
        /// <summary>
        /// Delete the specified service.
        /// </summary>
        /// <param name="service">The service.</param>
        public static RUS_Service Delete(RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_ServiceDetail sd in service.RUS_ServiceDetails)
                {
                    if (sd.ServiceDetail_trash == 0)
                        throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                }
                service.Service_trash = 1;
                try
                {
                    //context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
            }
            return service;
        }

        /// <summary>
        /// Restore the specified service.
        /// </summary>
        /// <param name="service">The service.</param>
        public static RUS_Service Restore(RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service == null)
                    return null;
                service.Service_trash = 0;
                try
                {
                    //context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
            }
            return service;
        }

        /// <summary>
        /// Delete the specified Service by ID.
        /// </summary>
        /// <param name="id">The service's id.</param>
        public static RUS_Service Delete(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_ID == id)
                            select s;
                RUS_Service service = query.SingleOrDefault<RUS_Service>();
                foreach (RUS_ServiceDetail sd in service.RUS_ServiceDetails)
                {
                    if (sd.ServiceDetail_trash == 0)
                        throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                }
                service.Service_trash = 1;
                try
                {
                    //context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
                return service;
            }
        }

        /// <summary>
        /// Restores the specified service by ID.
        /// </summary>
        /// <param name="id">The service's id.</param>
        public static RUS_Service Restore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_ID == id)
                            select s;
                RUS_Service service = query.SingleOrDefault<RUS_Service>();
                service.Service_trash = 0;
                try
                {
                    //context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return service;
            }
        }

        /// <summary>
        /// Deletes multiple specified services.
        /// </summary>
        /// <param name="ids">The services' ids.</param>
        public static List<RUS_Service> Delete(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (ids.Contains(s.Service_ID))
                            select s;
                List<RUS_Service> list = query.ToList<RUS_Service>();
                foreach (RUS_Service s in list)
                {
                    foreach (RUS_ServiceDetail sd in s.RUS_ServiceDetails)
                        if (sd.ServiceDetail_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    s.Service_trash = 1;
                }
                try
                {
                    //context.RUS_Services.AttachAll<RUS_Service>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
                return list;
            }
        }

        /// <summary>
        /// Restores multiple specified services.
        /// </summary>
        /// <param name="ids">The ids.</param>
        public static List<RUS_Service> Restore(int[] ids)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (ids.Contains(s.Service_ID))
                            select s;
                List<RUS_Service> list = query.ToList<RUS_Service>();
                foreach (RUS_Service s in list)
                {
                    s.Service_trash = 0;
                }
                try
                {
                    //context.RUS_Services.AttachAll<RUS_Service>(list);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return list;
            }
        }

        /// <summary>
        /// Deletes multiples specified services.
        /// </summary>
        /// <param name="services">The services.</param>
        public static List<RUS_Service> Delete(List<RUS_Service> services)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Service s in services)
                {
                    foreach (RUS_ServiceDetail sd in s.RUS_ServiceDetails)
                        if (sd.ServiceDetail_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    s.Service_trash = 1;
                }
                try
                {
                    //context.RUS_Services.AttachAll<RUS_Service>(services);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể xóa dịch vụ được.");
                }
                return services;
            }
        }

        /// <summary>
        /// Restores multiple specified services.
        /// </summary>
        /// <param name="ids">The ids.</param>
        public static List<RUS_Service> Restore(List<RUS_Service> services)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                foreach (RUS_Service s in services)
                {
                    s.Service_trash = 0;
                }
                try
                {
                    //context.RUS_Services.AttachAll<RUS_Service>(services);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return services;
            }
        }

        /// <summary>
        /// Deletes or restores Service.
        /// </summary>
        /// <param name="service">The service.</param>
        public static RUS_Service DeleteRestore(RUS_Service service)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                if (service.Service_trash == 0)
                {
                    foreach (RUS_ServiceDetail sd in service.RUS_ServiceDetails)
                    {
                        if (sd.ServiceDetail_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    }
                }
                service.Service_trash = 1 - service.Service_trash;
                try
                {
                  //  context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return service;
            }
        }

        /// <summary>
        /// Deletes or restores Service.
        /// </summary>
        /// <param name="id">The id.</param>
        public static RUS_Service DeleteRestore(int id)
        {
            using (Breakers_RyderUSDataContext context = new Breakers_RyderUSDataContext(conString))
            {
                var query = from s in context.RUS_Services
                            where (s.Service_ID == id)
                            select s;
                RUS_Service service = query.SingleOrDefault<RUS_Service>();
                if (service.Service_trash == 0)
                {
                    foreach (RUS_ServiceDetail sd in service.RUS_ServiceDetails)
                    {
                        if (sd.ServiceDetail_trash == 0)
                            throw new Exception("Không thể xóa vì tồn tại dữ liệu liên quan.");
                    }
                }
                service.Service_trash = 1 - service.Service_trash;
                try
                {
                 //   context.RUS_Services.Attach(service);
                    context.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw new Exception("Có lỗi xảy ra! Không thể phục hồi dịch vụ được.");
                }
                return service;
            }
        }
        #endregion
    }
}
